<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="example">
    <p>Original message: "{{ message }}"</p>
    <p>Computed reversed message: "{{ reversedMessage }}"</p>
    <p>Computed reversed message: "{{ reversedMessage1() }}"</p>
    <p>Computed reversed message: "{{ now }}"</p>
    <p>Computed reversed message: "{{ now1() }}"</p>


</div>
<div id="demo">{{ fullName }}</div>
<div id="demo1">{{ fullName }}</div>
<div id="demo2">{{ fullName }}</div>

<script>

    var example = new Vue({
        el: '#example',
        data: {
            message: 'Hello'
        },
        computed: {
            // 计算属性的 getter
            reversedMessage: function () {
                // `this` 指向 vm 实例
                return this.message.split('').reverse().join('')
            },
            now: function () {
                return Date.now()
            }
        },
        methods:{
            reversedMessage1:function(){
                return this.message.split('').reverse().join('')
            },
            now1: function () {
                return Date.now()
            }
        }
    })

    var demo = new Vue({
        el: '#demo',
        data: {
            firstName: 'Foo',
            lastName: 'Bar',
            fullName: 'Foo Bar'
        },
        watch: {
            firstName: function (val) {
                this.fullName = val + ' ' + this.lastName
            },
            lastName: function (val) {
                this.fullName = this.firstName + ' ' + val
            }
        }
    })
    //更好的方式
    var demo1 = new Vue({
        el: '#demo1',
        data: {
            firstName: 'Foo',
            lastName: 'Bar'
        },
        computed: {
            fullName: function () {
                return this.firstName + ' ' + this.lastName
            }
        }
    })

    var demo2 = new Vue({
        el: '#demo2',
        data: {
            firstName: 'Foo',
            lastName: 'Bar'
        },
        computed: {
            fullName: {
                // getter
                get: function () {
                    return this.firstName + ' ' + this.lastName
                },
                // setter
                set: function (newValue) {
                    var names = newValue.split(' ')
                    this.firstName = names[0]
                    this.lastName = names[names.length - 1]
                }
            }
        }
    })

</script>

</body>
</html>